package com.maila.xiaoaoschool.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.activity.ShopActivity;
import com.maila.xiaoaoschool.entity.ShopClasses;

import java.util.List;

public class TypeAdapter extends RecyclerView.Adapter<TypeAdapter.ViewHolder> {
    public int selectTypeId;
    public ShopActivity activity;
    public List<ShopClasses.DataBean> dataList;
    int mIndex=0;
    public TypeAdapter(ShopActivity activity, List<ShopClasses.DataBean> dataList) {
        this.activity = activity;
        this.dataList = dataList;
    }

    public void setDataList(List<ShopClasses.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_type,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ShopClasses.DataBean item = dataList.get(position);

        holder.bindData(item,position);
    }

    /**
     * 设置当前的位置
     *
     * @param mIndex
     */
    public void setIndex(int mIndex) {
        this.mIndex = mIndex;
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        if(dataList==null){
            return 0;
        }
        return dataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView tvCount,type;
        private ShopClasses.DataBean item;
        int position;
        public ViewHolder(View itemView) {
            super(itemView);
            tvCount = (TextView) itemView.findViewById(R.id.tvCount);
            type = (TextView) itemView.findViewById(R.id.type);
            itemView.setOnClickListener(this);
        }

        public void bindData(ShopClasses.DataBean item,int position){
            this.item = item;
            this.position=position;
            type.setText(item.getTypename());
            int count = activity.getSelectedGroupCountByTypeId(item.getTypeid());
            tvCount.setText(String.valueOf(count));
            if(count<1){
                tvCount.setVisibility(View.GONE);
            }else{
                tvCount.setVisibility(View.VISIBLE);
            }
            if (position == mIndex) {
                itemView.setBackgroundColor(activity.getResources().getColor(R.color.white));
            } else {
                itemView.setBackgroundColor(activity.getResources().getColor(R.color.gray));
            }
//            if(item.getTypeid()==selectTypeId){
//                itemView.setBackgroundColor(Color.WHITE);
//            }else{
//                itemView.setBackgroundColor(Color.TRANSPARENT);
//            }

        }

        @Override
        public void onClick(View v) {
            activity.onTypeClicked(item.getTypeid());
            setIndex(position);

        }
    }
}
